package org.point.controller;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.point.common.controller.BaseController;
import org.point.common.util.DoPage;
import org.point.domain.ConvertRuleMain;
import org.point.domain.CreditsBehavior;
import org.point.domain.CreditsType;
import org.point.service.ConvertRuleMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @ClassName: ConvertRuleMainController  
 * @Description: TODO(兑换规则主表)  
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午9:18:01  
 *
 */
@Controller
@RequestMapping("/convertRM/")
public class ConvertRuleMainController extends BaseController {

	@Autowired
	private ConvertRuleMainService service;
	
	
	/**
	 * 
	 * @Title: crmlist 
	 * @Description: TODO(后台会员查询兑换规则数据) 
	 * @param @param createOper
	 * @param @param currentPage
	 * @param @param response    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "crmlist.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void crmlist(@RequestParam(value = "createoper")String createOper, @RequestParam(value = "currentPage") Integer currentPage, HttpServletResponse response){
		
		JSONObject jsonObj = new JSONObject();
		DoPage<ConvertRuleMain> page = new DoPage<ConvertRuleMain>();
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			if(strOperateUtil.isNullForString(createOper)){
				map.put("createOper", createOper);
				page.setMap(map);
				// 分页查询兑换规则记录，并分页返回
				DoPage<ConvertRuleMain>  resultPage = service.searchCRMForMem(page);
				// 查询成功，并返回分页数据
				jsonObj.put("page", resultPage);
				jsonObj.put("resultMsg", 1);
			} else {
				// 分页参数为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", 0);
			throw new RuntimeException("查询兑换规则错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员", e);
			}
		}
		
	}
	
	/**
	 * 
	 * @Title: getCreditsType 
	 * @Description: TODO(根据兑换规则ID，查询详细信息) 
	 * @param @param creditsTypeId    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "getCRM.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void getConvertRule(@RequestParam(value = "id")String id, HttpServletResponse response){
		
		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;
		
		try {
			if(strOperateUtil.isNullForString(id)){
				map.put("id", Integer.parseInt(id));
				ConvertRuleMain record = service.getCTByCRMID(map);
			
				jsonObj.put("record", record);
				// 查询积分规则详细信息，成功
				jsonObj.put("resultMsg", 1);
			}else{
				// 积分规则分类ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("查询兑换规则分类详细信息！错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	
	/**
	 * 
	 * @Title: crminsert 
	 * @Description: TODO(新增兑换规则) 
	 * @param @param record
	 * @param @param response    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "crminsert.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void crminsert(@ModelAttribute("ConvertRuleMain") ConvertRuleMain record, HttpServletResponse response){
		
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		String crmCode = getNextNo("CM");
		try {
			if(record != null){
				// 新增兑换规则记录
				result = service.insertCRM(record,crmCode);
				jsonObj.put("resultMsg", 1);
			} else {
				// 兑换规则参数为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("新增兑换规则", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	/**
	 * 
	 * @Title: editstatus 
	 * @Description: TODO(修改兑换规则状态) 
	 * @param @param status
	 * @param @param convertId
	 * @param @param response    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "editstatus.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void editstatus(@RequestParam(value = "status")String status, @RequestParam(value = "convertId")String convertId, HttpServletResponse response){
		
		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;
		try {
			if(strOperateUtil.isNullForString(convertId)){
				map.put("convertId", convertId);
				if(strOperateUtil.isNullForString(status)){
					map.put("status", status);
					result = service.updStatus(map);
					// 修改状态成功
					jsonObj.put("resultMsg", 1);
				} else {
					// 修改状态为null
					jsonObj.put("resultMsg", -2);
				}
			} else {
				// 用户ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("修改兑换规则状态", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	/**
	 * 
	 * @Title: delCT 
	 * @Description: TODO(批量删除积分规则分类) 
	 * @param @param record    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "delCRM.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void delCRM(@RequestParam(value = "ids") String ids, HttpServletResponse response){
		// 测试
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		String newIDs = ids.substring(9, ids.length()-1);
		try {
			map.put("newIDs", newIDs);
			result = service.deleteCRM(map);
			//result = service.deleteCT();
			if(result>0){
				// 积分规则分类新增成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 积分规则分类参数为null， 请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("兑换规则删除错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	
	/**
	 * 
	 * @Title: editdelflag 
	 * @Description: TODO(修改兑换规则删除状态) 
	 * @param @param delflag
	 * @param @param convertId
	 * @param @param response    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "editdelflag.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void editdelflag(@RequestParam(value = "delflag")String delflag, @RequestParam(value = "convertId")String convertId, HttpServletResponse response){
		
		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;
		try {
			if(strOperateUtil.isNullForString(convertId)){
				map.put("convertId", convertId);
				if(strOperateUtil.isNullForString(delflag)){
					map.put("delflag", delflag);
					result = service.updDelflag(map);
					// 修改状态成功
					jsonObj.put("resultMsg", 1);
				} else {
					// 修改状态为null
					jsonObj.put("resultMsg", -2);
				}
			} else {
				// 用户ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("修改兑换规则删除状态", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	
	
}
